home *** CD-ROM | disk | FTP | other *** search
-
- #include "CIncludesCode.h"
- #include <Memory.h>
- #include <StdIO.h>
-
-
- extern long totalWords;
- extern long numFiles;
- extern ptrArray *argvPtr;
- extern strArray **filesHdl;
- extern Handle dictionary[numDictionaries];
- extern Handle dependencies;
-
-
-
- short dictionaryIndex( char ch )
- {
- if ( ch == '_' )
- return 0;
- else
- return ( (ch & 0xDF) - '@' );
- }
-
-
- void insertWord( short fileIndex, long filePosition, char *s )
- {
- Handle dictHdl;
- long curSize;
- long endOffset;
- char *p;
- short len = strlen( s );
-
- if ( len == 0 ) return;
-
- totalWords++;
-
- dictHdl = dictionary[ dictionaryIndex( *s ) ];
- curSize = GetHandleSize( dictHdl );
- endOffset = *((long*) (*dictHdl));
-
- if ( ( 8 + endOffset + len + 1 ) > curSize )
- {
- SetHandleSize( dictHdl, curSize + 1024 );
- if ( MemError() )
- {
- fprintf( stderr, "MemError = %d\n", MemError() );
- errorExit ( "Could not resize dictionary handle!" );
- }
- }
-
- p = *dictHdl + endOffset;
- putShort( p, fileIndex );
- putLong( (p + 2), filePosition );
- strcpy( (p + (2 + 4) ), s );
-
- *((long*) (*dictHdl)) += ( (2 + 4 + 1) + len); // fileIndex = 2, filePostion = 4, '\0' = 1
- }
-
-
- #define W(s) insertWord( -1, 0, s )
-
- void fillReservedWords()
- {
- W("asm"); W("double"); W("int"); W("sizeof");
- W("auto"); W("else"); W("long"); W("static");
- W("break"); W("entry"); W("new"); W("struct");
- W("case"); W("enum"); W("operator"); W("switch");
- W("catch"); W("extended"); W("overload"); W("template");
- W("char"); W("extern"); W("pascal"); W("this");
- W("class"); W("float"); W("private"); W("typedef");
- W("comp"); W("for"); W("protected"); W("union");
- W("const"); W("friend"); W("public"); W("unsigned");
- W("continue"); W("goto"); W("register"); W("virtual");
- W("default"); W("if"); W("return"); W("void");
- W("delete"); W("inherited"); W("short"); W("volatile");
- W("do"); W("inline"); W("signed"); W("while");
- }
-
-
- void initDictionaries()
- {
- short i;
-
- fprintf( stderr, "Initializing dictionaries...\n" );
-
- for ( i = 0; i < numDictionaries; ++i )
- {
- dictionary[i] = NewHandle( 4 );
- *((long*) (*dictionary[i])) = 4; /* "End Pointer" */
- }
-
- fillReservedWords();
- }
-
-
- void DisposDictionaries()
- {
- short i;
-
- for ( i = 0; i < numDictionaries; ++i )
- DisposHandle( dictionary[i] );
- }
-
-
- char *extractIdentifier( char *dest, char *src)
- {
- char *destPtr = dest;
- short count;
-
- for ( count = 0; validChar( *src ) && (count < 64); ++count )
- *dest++ = *src++;
- if ( *src == '.' ) // return NULL string for structure members, etc.
- {
- while ( (*src == '.') || validChar( *src ) )
- src++;
- *destPtr = '\0';
- }
- else
- *dest = '\0';
-
- return src;
- }
-
-
- long nextIdentifier( char *s, Handle dataHdl, long offset, long totalSize )
- { // parse text looking for valid identifiers
-
- char *base = StripAddress(*dataHdl);
- char *p = base + offset;
- char *q = base + totalSize;
- char ch;
-
- while ( p < q )
- {
- ch = *p++;
-
- if ( ch == '"' ) // skip string literals
- while ( (*p++ != '"') || (*(p-2) == '\\') ) ;
-
- else if ( ch == '#' ) // skip pre-processor commands
- while ( validChar(*p++) ) ;
-
- else if ( (ch == '/') && (*p == '/') ) // skip comments
- while ( *p++ != '\n' ) ;
-
- else if ( (ch == '/') && (*p == '*') ) // skip comments
- while ( (*p++ != '/') || (*(p-2) != '*') ) ;
-
- else if ( validStart( ch ) )
- {
- p = extractIdentifier( s, p - 1 );
- if ( *s )
- return ( (long) ( p - base ) );
- }
- }
- return totalSize;
- }
-
-
-
- long searchDict( char *s, Handle dictHdl ) // returns offset or 0
- {
- char *base = StripAddress(*dictHdl);
- char *p = base + (4 + 2 + 4); // endOffset = 4, fileIndex = 2, filePostion = 4;
- char *limit = base + *((long*) base);
-
- if ( *s )
- while ( p < limit )
- if ( strcmp( p, s ) == 0 )
- return ( (long) (p - base - (2 + 4) ) ); // fileIndex = 2, filePostion = 4
- else
- p += strlen( p ) + (2 + 4 + 1); // fileIndex = 2, filePostion = 4, '\0' = 1
- return 0;
- }
-
-
- long parseFile( Handle dataHdl, short fileIndex )
- {
- short oldFile;
- long offset;
- long count = 0;
- long pos = 0;
- long totalSize = GetHandleSize( dataHdl ) - 1;
- Handle dictHdl;
- char s[64];
-
- while ( (pos = nextIdentifier( s, dataHdl, pos, totalSize )) < totalSize )
- {
- dictHdl = dictionary[dictionaryIndex( *s )];
-
- if ( offset = searchDict( s, dictHdl ) )
- {
- oldFile = getShort( *dictHdl + offset );
-
- if ( (oldFile != -1) &&
- (oldFile != fileIndex) &&
- isDependent( *dependencies, oldFile, fileIndex ) )
- {
- putShort( (*dictHdl + offset), fileIndex );
- putLong( (*dictHdl + offset + 2), pos );
- }
- }
- else
- {
- count++;
- insertWord( fileIndex, pos, s );
- }
- }
- return count;
- }
-
-
- void fillDictionaries()
- {
- short i;
- short maxLength = maxFilename();
- long count = 0;
- Handle dataHdl;
-
- fprintf( stderr, "\nParsing Files:\n" );
-
- for ( i = 0; i < numFiles; ++i )
- {
- fprintf( stderr, " %-*s", maxLength, (**filesHdl)[i] );
-
- dataHdl = loadDataFile( (*argvPtr)[i + 2] );
- count = parseFile( dataHdl, i );
- DisposHandle( dataHdl );
-
- fprintf( stderr, " %4d new\n", count);
- }
-
- fprintf( stderr, "\nTotal Entries = %ld\n", totalWords );
- }
-
-
- void writeDictionary( Handle dictHdl )
- {
- long pos = 4;
- long endOffset = *((long*) (*dictHdl));
- char s[64];
-
- while ( pos < endOffset )
- {
- fprintf( stderr, "%3d ", getShort( *dictHdl + pos ) );
- pos += 2;
- strcpy( s, *dictHdl + pos);
- fprintf( stderr, "%s\n", s );
- pos += strlen( s ) + 1;
- }
- }
-
-
- void writeAllDictionaries()
- {
- short i;
-
- for ( i = 0; i < numDictionaries; ++i )
- writeDictionary( dictionary[i] );
- }
-
-
- void writeSpecificDirectory( char ch )
- {
- writeDictionary( dictionary[dictionaryIndex( ch )] );
- }
-
-